package com.magiccompass.alipay.barcode.util;

import com.magiccompass.alipay.barcode.pageform.AlipayQRCodeNotificationPageForm;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.TreeMap;

import com.magiccompass.alipay.barcode.entity.AlipayQRCodeNotification;
import com.magiccompass.alipay.barcode.entity.MerchantInfo;

public class AlipayQRCodeNotificationUtil {
	public static AlipayQRCodeNotification notificationPageForm2Entity(AlipayQRCodeNotificationPageForm pageForm){
		AlipayQRCodeNotification arqcn = new AlipayQRCodeNotification();
		arqcn.setBody(pageForm.getBody());
		arqcn.setBuyerEmail(pageForm.getBuyer_email());
		arqcn.setBuyerId(pageForm.getBuyer_id());
		arqcn.setCurrency(pageForm.getCurrency());
		arqcn.setExtraCommonParam(pageForm.getExtra_common_param());
		arqcn.setForexRate(pageForm.getForex_rate());
		arqcn.setForexTotalFee(pageForm.getForex_total_fee());
		arqcn.setGmtCreate(pageForm.getGmt_create());
		arqcn.setGmtPayment(pageForm.getGmt_payment());
		arqcn.setNotifyActionType(pageForm.getNotify_action_type());
		arqcn.setNotifyId(pageForm.getNotify_id());
		arqcn.setNotifyTime(pageForm.getNotify_time());
		arqcn.setNotifyType(pageForm.getNotify_type());
		arqcn.setOutBizNo(pageForm.getOut_biz_no());
		arqcn.setOutTradeNo(pageForm.getOut_trade_no());
		arqcn.setPaytoolsPayAmount(pageForm.getPaytools_pay_amount());
		arqcn.setPrice(pageForm.getPrice());
		arqcn.setQuantity(pageForm.getQuantity());
		arqcn.setRefundFee(pageForm.getRefund_fee());
		arqcn.setSellerEmail(pageForm.getSeller_email());
		arqcn.setSellerId(pageForm.getSeller_id());
		arqcn.setSign(pageForm.getSign());
		arqcn.setSignType(pageForm.getSign_type());
		arqcn.setSubject(pageForm.getSubject());
		arqcn.setTotalFee(pageForm.getTotal_fee());
		arqcn.setTradeNo(pageForm.getTrade_no());
		arqcn.setTransForexRate(pageForm.getTrans_forex_rate());
		arqcn.setTradeStatus(pageForm.getTrade_status());
		arqcn.setTransAmount(pageForm.getTrans_amount());
		arqcn.setTransCurrency(pageForm.getTrans_currency());
		
		return arqcn;
	}
	
	public static TreeMap<String,String> notificationPageForm2Map(AlipayQRCodeNotificationPageForm pageForm){
		TreeMap<String,String> returnMap = new TreeMap<String,String>();
		returnMap.put("notify_time", pageForm.getNotify_time());
		returnMap.put("notify_type", pageForm.getNotify_type());
		returnMap.put("notify_id", pageForm.getNotify_id());
		returnMap.put("sign_type", pageForm.getSign_type());
		returnMap.put("sign", pageForm.getSign());
		if(pageForm.getNotify_action_type()!=null && !pageForm.getNotify_action_type().isEmpty())
			returnMap.put("notify_action_type", pageForm.getNotify_action_type());
		if(pageForm.getOut_trade_no()!=null && !pageForm.getOut_trade_no().isEmpty())
			returnMap.put("out_trade_no", pageForm.getOut_trade_no());
		if(pageForm.getSubject()!=null && !pageForm.getSubject().isEmpty())
			returnMap.put("subject", pageForm.getSubject());
		if(pageForm.getTrade_no()!=null && !pageForm.getTrade_no().isEmpty())
			returnMap.put("trade_no", pageForm.getTrade_no());
		if(pageForm.getTrade_status()!=null && !pageForm.getTrade_status().isEmpty())
			returnMap.put("trade_status", pageForm.getTrade_status());
		if(pageForm.getGmt_create()!=null && !pageForm.getGmt_create().isEmpty())
			returnMap.put("gmt_create", pageForm.getGmt_create());
		if(pageForm.getGmt_payment()!=null && !pageForm.getGmt_payment().isEmpty())
			returnMap.put("gmt_payment", pageForm.getGmt_payment());
		if(pageForm.getSeller_email()!=null && !pageForm.getSeller_email().isEmpty())
			returnMap.put("seller_email", pageForm.getSeller_email());
		if(pageForm.getBuyer_email()!=null && !pageForm.getBuyer_email().isEmpty())
			returnMap.put("buyer_email", pageForm.getBuyer_email());
		if(pageForm.getSeller_id()!=null && !pageForm.getSeller_id().isEmpty())
			returnMap.put("seller_id", pageForm.getSeller_id());
		if(pageForm.getBuyer_id()!=null && !pageForm.getBuyer_id().isEmpty())
			returnMap.put("buyer_id", pageForm.getBuyer_id());
		if(pageForm.getPrice()!=null && !pageForm.getPrice().isEmpty())
			returnMap.put("price", pageForm.getPrice());
		if(pageForm.getQuantity()!=null && !pageForm.getQuantity().isEmpty())
			returnMap.put("quantity", pageForm.getQuantity());
		if(pageForm.getTotal_fee()!=null && !pageForm.getTotal_fee().isEmpty())
			returnMap.put("total_fee", pageForm.getTotal_fee());
		if(pageForm.getTrans_amount()!=null && !pageForm.getTrans_amount().isEmpty())
			returnMap.put("trans_amount", pageForm.getTrans_amount());
		if(pageForm.getForex_total_fee()!=null && !pageForm.getForex_total_fee().isEmpty())
			returnMap.put("forex_total_fee", pageForm.getForex_total_fee());
		if(pageForm.getCurrency()!=null && !pageForm.getCurrency().isEmpty())
			returnMap.put("currency", pageForm.getCurrency());
		if(pageForm.getTrans_currency()!=null && !pageForm.getTrans_currency().isEmpty())
			returnMap.put("trans_currency", pageForm.getTrans_currency());
		if(pageForm.getForex_rate()!=null && !pageForm.getForex_rate().isEmpty())
			returnMap.put("forex_rate", pageForm.getForex_rate());
		if(pageForm.getTrans_forex_rate()!=null && !pageForm.getTrans_forex_rate().isEmpty())
			returnMap.put("trans_forex_rate", pageForm.getTrans_forex_rate());
		if(pageForm.getBody()!=null && !pageForm.getBody().isEmpty())
			returnMap.put("body", pageForm.getBody());
		if(pageForm.getRefund_fee()!=null && !pageForm.getRefund_fee().isEmpty())
			returnMap.put("refund_fee", pageForm.getRefund_fee());
		if(pageForm.getOut_biz_no()!=null && !pageForm.getOut_biz_no().isEmpty())
			returnMap.put("out_biz_no", pageForm.getOut_biz_no());
		if(pageForm.getPaytools_pay_amount()!=null && !pageForm.getPaytools_pay_amount().isEmpty())
			returnMap.put("paytools_pay_amount", pageForm.getPaytools_pay_amount());
		if(pageForm.getExtra_common_param()!=null && !pageForm.getExtra_common_param().isEmpty())
			returnMap.put("extra_common_param", pageForm.getExtra_common_param());
		
		return returnMap;
	}
	
	public static void sendMechantMTSuccessEmail(MerchantInfo mi, AlipayQRCodeNotificationPageForm qrfm){
		EmailUtil eu = new EmailUtil();
		String receiverEmailAddress = mi.getEmail();
		StringBuffer sb = new StringBuffer();
		sb.append("Hi ").append(mi.getDisplayName());
		sb.append(", a new transaction has successful been paid. $").append(qrfm.getTrans_amount());
		String title = sb.toString();
		sb = new StringBuffer();
		sb.append("We have successful received payment from ").append(qrfm.getBuyer_email()).append(".\r\n");
		sb.append("This transaction amount is:").append(qrfm.getTrans_amount()).append(".\r\n");
		sb.append("Please check customer email or mobile number to verify customer.\r\n");
		sb.append("This transaction should be in \"Transaction Query\", if you need cancel or refund it.\r\n");
		sb.append("This is automatic email, please do not reply.\r\n");
		sb.append("If you have any further question, please contact our support.\r\n");
		String body = sb.toString();
		eu.sendSimpleEmail(receiverEmailAddress, title, body);
	}
}